package cn.community.care.module.system.service.basicinfo;

import cn.community.care.module.system.controller.admin.basicinfo.vo.BasicInfoPageReqVO;
import cn.community.care.module.system.controller.admin.basicinfo.vo.BasicInfoSaveReqVO;
import cn.community.care.module.system.dal.dataobject.basicinfo.BasicInfoDO;
import cn.community.care.module.system.dal.mysql.basicinfo.BasicInfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用户基本信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class BasicInfoServiceImpl implements BasicInfoService {

    @Resource
    private BasicInfoMapper basicInfoMapper;

    @Override
    public Long createBasicInfo(BasicInfoSaveReqVO createReqVO) {
        // 插入
        BasicInfoDO basicInfo = BeanUtils.toBean(createReqVO, BasicInfoDO.class);
        basicInfoMapper.insert(basicInfo);
        // 返回
        return basicInfo.getId();
    }

    @Override
    public void updateBasicInfo(BasicInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicInfoExists(updateReqVO.getId());
        // 更新
        BasicInfoDO updateObj = BeanUtils.toBean(updateReqVO, BasicInfoDO.class);
        basicInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicInfo(Long id) {
        // 校验存在
        validateBasicInfoExists(id);
        // 删除
        basicInfoMapper.deleteById(id);
    }

    private void validateBasicInfoExists(Long id) {
        if (basicInfoMapper.selectById(id) == null) {
//            throw exception(BASIC_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BasicInfoDO getBasicInfo(Long id) {
        return basicInfoMapper.selectById(id);
    }

    @Override
    public BasicInfoDO getBasicInfoByUserID(Long id) {
        return basicInfoMapper.selectOne(BasicInfoDO::getUserId, id);
    }


    @Override
    public PageResult<BasicInfoDO> getBasicInfoPage(BasicInfoPageReqVO pageReqVO) {
        return basicInfoMapper.selectPage(pageReqVO);
    }

}